
//
//  YXMachineMaterialTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMachineMaterialTableView.h"
#import "YXMachineMaterialCell.h"
#import "YXMachinesModel.h"
#import "YXWorkOrderModel.h"
@interface YXMachineMaterialTableView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong) YXWorkOrderModel *model;
@end
@implementation YXMachineMaterialTableView


- (void)setTitle:(NSString *)title {
    _title = title;
    
    NSLog(@"%@",_title);
}

- (void)setDataArr:(NSArray *)dataArr {
    _dataArr = dataArr;
    if ([_title isEqualToString:@"我的工单"]) {
        for (YXWorkOrderModel *model in _dataArr) {
            self.model = model;
        }
    }else {
        _dataArr = [YXMachinesModel mj_objectArrayWithKeyValuesArray:_dataArr];
    }
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = color_backColor;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
        self.dataSource = self;
        self.delegate = self;
    }
    return self;
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellId = @"YXMachineMaterialCell";
    YXMachineMaterialCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil] lastObject];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if ([self.title isEqualToString:@"我的工单"]) {
        cell.workModel = self.dataArr[indexPath.section];
    }else {
        cell.model = self.dataArr[indexPath.section];
    }
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 262;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if ([self.title isEqualToString:@"我的工单"]) {
        UIView *sectionView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 40))];
        sectionView.backgroundColor = [UIColor whiteColor];
        
        UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 1))];
        lineView.backgroundColor = color_lineColor;
        [sectionView addSubview:lineView];
        
        NSString *time = [UILabel formateData:self.model.createTime];
        UILabel *timeLab = [UILabel setLabelWithText:[NSString stringWithFormat:@"创建时间：%@",time] fontSize:12 color:color_textThree];
        [sectionView addSubview:timeLab];
        [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(sectionView.mas_left).offset(10);
            make.centerY.equalTo(sectionView.mas_centerY);
            [timeLab sizeToFit];
        }];
        
        UILabel *orderID = [UILabel setLabelWithText:[NSString stringWithFormat:@"工单编号：%@",self.model.orderId] fontSize:12 color:color_textThree];
        [sectionView addSubview:orderID];
        [orderID mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(sectionView.mas_right).offset(-10);
            make.centerY.equalTo(sectionView.mas_centerY);
            [orderID sizeToFit];
        }];
        
        return sectionView;
    }else {
        return [[UIView alloc] init];
    }
    
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if ([self.title isEqualToString:@"我的工单"]) {
        return 40;
    }else {
        return 0;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectRowAtIndexPath) {
        self.selectRowAtIndexPath(indexPath.section,self.title,self.dataArr[indexPath.section]);
    }
}

#pragma mark - 取消顶部悬浮效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

@end
